import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.OutputStream;

public class CodeGenerator {
    public static final void main(String args[]) {
        if(args == null) {
            System.out.println("Usage: java CodeGenerator ModuleName");
            return;
        }

        String module = args[0];
        char c = module.charAt(0);
        if(!Character.isUpperCase(c)) {
            module = Character.toUpperCase(c) + module.substring(1);
        }

        StringBuilder sb = new StringBuilder();

        //import
        sb.append("import javassist.*;\n");
        sb.append("import java.util.*;\n");

        //class declaration
        sb.append("public class Extract" + module + " {\n");

        //constructor
        sb.append("    public Extract" + module + "(String dir) {\n");
        sb.append("        this.dir = dir;\n");
        sb.append("        init();\n");
        sb.append("    }\n\n");

        sb.append("    public /*TODO:*/ getResult() {\n");
        sb.append("        doIt();\n");
        sb.append("        return result;\n");
        sb.append("    }\n\n");

        sb.append("    private void doIt() {\n");
        sb.append("        JavassistAdapter ja = new JavassistAdapter(dir);\n");
        sb.append("        List<String> classes = ja.getResult();\n\n");
        sb.append("        Iterator<String> iter = classes.iterator();\n");
        sb.append("        while( iter.hasNext() ) {\n");
        sb.append("            String path = iter.next();\n\n");
        sb.append("            CtClass cc = null;\n");
        sb.append("            try {\n");
        sb.append("                cc = pool.get(path);\n");
        sb.append("            } catch(NotFoundException nfe) {\n");
        sb.append("                nfe.printStackTrace();\n");
        sb.append("                continue;\n");
        sb.append("            } catch(Exception e) {\n");
        sb.append("                e.printStackTrace();\n");
        sb.append("                return;\n");
        sb.append("            }\n");
        sb.append("            \n");
        sb.append("            /*TODO:*/\n");
        sb.append("            \n");
        sb.append("        }\n");
        sb.append("    }\n\n");

        sb.append("    private void init() {\n");
        sb.append("        try {\n");
        sb.append("            pool.appendPathList(this.dir);\n");
        sb.append("        } catch(Exception e) {\n");
        sb.append("            e.printStackTrace();\n");
        sb.append("        }\n");
        sb.append("    }\n\n");

        sb.append("    private String dir;\n");
        sb.append("    private /*TODO*/ result;\n");
        sb.append("    private ClassPool pool = new ClassPool(false);//ClassPool.getDefault();\n");

        sb.append("}\n");

        //write sb to ExtractModule.java file
        File f = new File("Extract" + module + ".java");
        if( f.exists() )
            f.delete();

        DataOutputStream os = null; 

        try {
            os = new DataOutputStream(
                    new BufferedOutputStream(
                        new FileOutputStream(f)));

            os.writeBytes(sb.toString());
            os.close();
        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}
